<?php
/*
 * 更新数据的处理文件
 * @author  Mark
 * @package ixdcw
 * @file    html.inc.php
 * @time    12-12-13 下午4:03
 */
defined('IN_IXDCW') or exit('Access Denied');
$menus        = array(
	array('项目更新', '?moduleid=' . $moduleid . '&file=' . $file),
);
$all          = (isset($all) && $all) ? 1 : 0;
$one          = (isset($one) && $one) ? 1 : 0;
$this_forward = '?moduleid=' . $moduleid . '&file=' . $file;
$table        = Table::name(Table::NAME_AUTO_MODEL);
switch ($action) {
	case 'url':
		// 是否更新
		$update = (isset($update) && $update) ? 1 : 0;

		// 最小的更新id
		if (!isset($fid)) {
			$r   = $db->getOne("SELECT min(amid) AS fid, max(amid) AS tid FROM {$table}");
			$fid = $r['fid'] ? $r['fid'] : 0;
			$tid = $r['tid'] ? $r['tid'] : 0;
		}

		// start id
		isset($sid) or $sid = $fid;

		isset($num) or $num = 100;
		if ($fid <= $tid) {
			$result = $db->query("SELECT amid FROM {$table} WHERE amid>=$fid {$sql} ORDER BY amid LIMIT 0,$num ");
			if ($db->affectedRows($result)) {
				while ($r = $db->fetchArray($result)) {
					$amid = $r['amid'];
					$url  = "linkurl='" . url('auto/model', 'modelid='.$amid) . "'";
					$db->query(Sql::update($table, $url, "amid=$amid"));
				}
				$amid += 1;
			} else {
				$amid = $fid + $num;
			}
		} else {
			msg('模块连接生成成功', $this_forward);
		}
		msg('ID从' . $fid . '至' . ($amid - 1) . $MOD['name'] . ($update ? '更新' : '生成') . '成功' . progress($sid, $fid, $tid), "?moduleid=$moduleid&file=$file&action=$action&sid=$sid&fid=$amid&tid=$tid&num=$num&update=$update&all=$all&one=$one");
		break;
	case "priceRange":
		$table        = Table::name(Table::NAME_AUTO_CATEGORY);
		$tableSource  = Table::name(Table::NAME_AUTO_MODEL);
		$tableRankCar = Table::name(Table::NAME_AUTO_RANK_CAR);
		// 最小的更新id
		if (!isset($fid) || !isset($tid)) {
			$r   = $db->getOne("SELECT min(arid) AS fid,  max(arid) AS tid FROM {$table} where typename='rank' and level=2");
			$fid = $r['fid'] ? $r['fid'] : 0;
			$tid = $r['tid'] ? $r['tid'] : 0;
		}

		// start id
		isset($sid) or $sid = $fid;

		isset($num) or $num = 10;
		if ($fid <= $tid) {
			$result = $db->query("SELECT arid FROM {$table} WHERE arid>=$fid ORDER BY arid LIMIT 0,$num ");
			if ($db->affectedRows($result)) {
				while ($r = $db->fetchArray($result)) {
					$arid = $r['arid'];
					$sql  = "select * from {$tableSource} where issale=1 and arcid=$arid";
					$re   = $db->getAll($sql);
					if ($re) {
						$priceRange   = Auto::priceRange($re);
						$levelRange   = Auto::levelRange($re);
						$gearboxRange = Auto::gearboxRange($re);
						$url        = "pricerange='" . $priceRange . "'";
						$info = array(
							'pricerange' => $priceRange,
							'levelrange' => $levelRange,
							'gearboxrange' => $gearboxRange,
						);
						$has = $db->getOne("select arid from $tableRankCar where arid = $arid");
						if ($has){
							$sql = Sql::update($tableRankCar, $info, "arid=$arid");
						} else {
							$info['arid'] = $arid;
							$sql = Sql::insert($tableRankCar, $info);
						}
						$db->query($sql);
					}
				}
				$arid += 1;
			} else {
				$arid = $fid + $num;
			}
		} else {
			msg('车系的范围生成成功', $this_forward);
		}
		msg('ID从' . $fid . '至' . ($arid - 1) . '车系的范围更新成功' . progress($sid, $fid, $tid), "?moduleid=$moduleid&file=$file&action=$action&sid=$sid&fid=$arid&tid=$tid&num=$num&update=$update&all=$all&one=$one");

		break;
	// 显示
	default:
		include tpl('html', $module);
		break;
}